<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'buyanimal';
$return=array();
$return['setting']=$this->plugin_setting('basic');
$data=array('1','3','5');
if($_RQ['op']=='buyanimal'){
	//租赁家禽
	if(!$_RQ['id']){
		exi('没有找到该家禽，请重新选择！','error','jy_farmer/aclassify/list');
	}
	$sendtype=$return['selectstore']?0:1;
	$animal=pdo_get('jy_farmer_animal',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	if($animal['down_send_type1']!=1 && $return['setting']['down_send_type1']!=1){
		$return['sendtypes'][1]='快递配送';
		$sendtype=1;
	}
	if($animal['down_send_type0']!=1 && $return['setting']['down_send_type0']!=1){
		$return['sendtypes'][0]='店内自提';
		$sendtype=0;
	}
	$aclassify=pdo_get('jy_farmer_aclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$animal['classify_id']));
	$this->session('animal_id',$_RQ['id']);
	$return['animal']=$animal;
	$return['data']=$data;
	$return['aclassify']=$aclassify;
	$return['selectstore']=$return['setting']['stores'][0]?:array();
	$post=array('sendtype'=>$sendtype,'total'=>1,'pcd'=>array('city'=>'深圳市'),'peisongfee'=>$return['setting']['peisongfee']);
	//刷新运费
	$up_dispatch=$this->model('dispatch')->up_dispatch($post);
	$post=$up_dispatch['post']?:$post;
	$post['peisongfee']=$post['dispatchprice']?:$post['peisongfee'];
	$return['post']=$post;
	$return['address']=$post['address'];
	$order=pdo_get('jy_farmer_order',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('username','mobile'),'id desc');
	$return['post']['username']=$order['username'];
	$return['post']['mobile']=$order['mobile'];
	if($post['sendtype']==1){
		$return['post']['endprice']=$animal['price']*$post['total']+$post['peisongfee'];
	}else{
		$return['post']['endprice']=$animal['price']*$post['total'];
	}
	$return['goodsname']='家禽';
	$return['province']=cfc('tools')->pcd();
	set_html('title','下单页面');
	exi($return);
}

if($_RQ['op']=='notice'){
	$post=get_postdata();
	$animal_id = $this->session('animal_id');
	if($post){
			if($post['sendtype']!=1 && empty($post['username'])){
				exi('请输入姓名','error');
			}
			if($post['sendtype']!=1 && empty($post['mobile'])){
				exi('请输入联系电话','error');
			}
			if($post['sendtype']!=1 && !preg_match("/^1[345789]\d{9}$/ims",$post['mobile'])){  	 
				exi('请输入正确的手机号码','error');
			}
			if($post['sendtype']==1 && (!$post['pcd']['province'] || !$post['address'])){
				exi('请填收货地址！','error');
			}
			$animal=pdo_get('jy_farmer_animal',array('uniacid'=>$_SESSION['uniacid'],'id'=>$animal_id));
			$aclassify=pdo_get('jy_farmer_aclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$animal['classify_id']));
			if($post['sendtype']==1){
				$money=$animal['price']*$post['total']+$post['peisongfee'];
			}else{
				$money=$animal['price']*$post['total'];
			}
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'mid'=>$_SESSION['uid'],
				'animal_id'=>$this->session('animal_id'),
				'username'=>$post['username']?:$post['address']['name'],
				'mobile'=>$post['mobile']?:$post['address']['mobile'],
				'address'=>$post['address']['pcd']['province'].' '.$post['address']['pcd']['city'].' '.$post['address']['pcd']['district'].' '.$post['address']['pcd']['street'].' '.$post['address']['address'],
				'time'=>$data[$post['timeid']],
				'money'=>$money,
				'sendtype'=>$post['sendtype'],
				'peisongfee'=>$return['setting']['peisongfee'],
				'ordersn'=>get_ordersn('jy_farmer_order','ordersn'),
				'total'=>$post['total'],
				'getstore'=>$post['sendtype']==1?'':ijson_encode($return['setting']['stores'][$post['storekey']]),
				'createtime'=>TIMESTAMP,
			);
			pdo_insert('jy_farmer_order',$insert);
			$id=pdo_insertid();
			$params['fee']=$insert['money'];
			$params['title']='领养'.$animal['title'];
			$params['id']=$id;
			$params['plugin']='jy_farmer';
			$params['back_url']='jy_farmer/buyanimal/payresult';
			$payid=cfc('pay')->create_pay_order($params);
			$this->pdo_update('order',array('pay_id'=>$payid),array('id'=>$id));
			$res=array(
				'pay_id'=>$payid,
				'order_id'=>$id,
			);
			exi($res);
	}
   else{
	   exi('请完善购买信息','error');
   }
	
}
if($_RQ['op']=='payresult'){
	$payresult=$_RQ['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','list');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','list');
	}
	$order=pdo_get('jy_farmer_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['money']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_farmer_order',array('status'=>1,'pay'=>$payresult['fee']),array('id'=>$order['id']));
	$num=pdo_count('jy_farmer_adopt',array('uniacid'=>$_SESSION['uniacid'],'animal_id'=>$order['animal_id']))+1;
	$order['total']=$order['total']?:1;
	for($i=0;$i<$order['total'];$i++){
		$insert=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$_SESSION['uid'],
			'animal_id'=>$order['animal_id'],
			'order_id'=>$order['id'],
			'number'=>$num,
			'createtime'=>TIMESTAMP,
		);
		$num++;
		pdo_insert('jy_farmer_adopt',$insert);
	}
	pdo_update('jy_farmer_animal',array('kucun -='=>$order['total']),array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['animal_id']));
	$this->model('order')->pay($order);
	exi('支付成功','success','jy_farmer/myanimal/wait/');
}

if($_RQ['op']=='money'){
	
	if($_RQ['id']===''){
		exi('请选择家禽','error');
	}
	$animal=pdo_get('jy_farmer_animal',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$aclassify=pdo_get('jy_farmer_aclassify',array('uniacid'=>$_SESSION['uniacid'],'id'=>$animal['classify_id']));
	$money=$data[$_RQ['time']]*$aclassify['price'];
	$return['money']=$money;
	$return['success']=1;
	exi($return);
}